package com.dragon.hash;

import java.util.HashMap;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/5/21 12:18
 */
public class SolutionTest {

    /**
     * 给定一个字符串，找到它的第一个不重复的字符，并返回它的索引。如果不存在，则返回 -1。
     * s = "leetcode"
     * 返回 0.
     * <p>
     * s = "loveleetcode",
     * 返回 2.
     */

    public int firstUniqChar(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (Character character : s.toCharArray()) {
            map.put(character, map.containsKey(character) ? map.get(character) + 1 : 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (map.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        SolutionTest solutionTest = new SolutionTest();
        System.out.println(solutionTest.firstUniqChar("loveleetcode"));
    }
}
